<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends MY_Controller {

	
	public function home($child_id)
	{
        $this->load->model('User_child_model');
        $child_info = $this->User_child_model->get_info_by_id($child_id);
        if($child_info){
            //孩子信息
            
            //统计习惯数量
            $habit_total = $this->User_child_model->get_habit_count($child_id);
            $grade_array = array(
                '0'=>'学前班',
                '1'=>'一年级',
                '2'=>'二年级', 
                '3'=>'三年级', 
                '4'=>'四年级', 
                '5'=>'五年级', 
                '6'=>'六年级', 
                '7'=>'七年级', 
                '8'=>'八年级', 
                '9'=>'九年级', 
                '10'=>'高一', 
                '11'=>'高二', 
                '12'=>'高三', 
                '13'=>'其他', 
            );
            
            //返回数组
            $info = array(
                'child_id' => $child_id, 
                'child_name' => $child_info['nick'],
                'child_img' => $child_info['head_img_url'],
                'gender' => $child_info['gender'],
                'grade' => $grade_array[$child_info['grade']],
                'stars_total' => $child_info['stars'],
                'habit_total' => $habit_total,
                'reward_total' => 0,
                'learning_score' => "5.0"
            );

            $this->smarty->assign("child_id", $child_id);
            $this->smarty->assign("info", $info);
            $this->smarty->display("h5/child_home.html");    
        }else{
            show_error("404 data not defined");
        }
		
	}

	public function habit($habit_id){
        $this->load->model("Habits");
        $task_info = $this->Habits->get_habit_info($habit_id);
        if(!$task_info){
            show_error("404 data not defined");
            exit;
        }
        $week_result = $this->Habits->get_week_habit_list($habit_id);
        $first=1;  
        $sdefaultDate = date("Y-m-d");
        $w=date('w',strtotime($sdefaultDate));
        $week_start_time=strtotime(date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days')));
        $week_data = array();
        for($i = 0; $i < 7; $i++){
            $week_data[$i]['today_start_time'] = $week_start_time + 3600 * 24 * $i;
            $week_data[$i]['today_end_time'] = $week_start_time + 3600 * 24 * ($i+1);
            $week_data[$i]['date'] = date("Y-m-d", $week_data[$i]['today_start_time']);
        }
        // print_r($week_result);
        $weeks = array("一", "二", "三", "四", "五", "六", "日");
        if(count($week_result)){
            foreach ($week_result as $key => $value) {
                foreach ($week_data as $k => $v) {
                    if(empty($v['record_id'])){
                        if($value['time'] >= $v['today_start_time'] && $value['time'] < $v['today_end_time']){
                            $value['date'] = $v['date'];
                            $week_data[$k]['record_id'] = $value['id'];
                            $week_data[$k]['stars'] = $value['stars'];
                        }else{
                            $week_data[$k]['record_id'] = 0;
                            $week_data[$k]['stars'] = 0;
                        }
                        $week_data[$k]['week'] = $weeks[$k];
                    }
                }
            }
        }else{
            foreach ($week_data as $k => $v) {
                $week_data[$k]['week'] = $weeks[$k];
                $week_data[$k]['record_id'] = 0;
                $week_data[$k]['stars'] = 0;
            }
        }
        $week = date('N', time());
        $task_info['today_week'] = $weeks[$week-1];

        $statistics_info = array("max_continuity" => 0, "total_stars" => 0, "continuity" => 0, "continuity_type" => 0);
        $statistics = get_field_by_where("user_children_task_type_statistics", "*", "task_id='{$habit_id}'");
        if($statistics){
            $statistics_info['total_stars'] = $statistics['total_stars'];
            $statistics_info['continuity'] = $statistics['continuity'];
        }
        $max_continuity = get_field_by_where("user_children_max_continuity_record", "continuity,end_time", "task_id='{$habit_id}' AND child_id = '{$task_info['child_id']}' ORDER BY continuity DESC");
        if($max_continuity){
            $statistics_info['max_continuity'] = $max_continuity['continuity'];
        }

        $last_continuity = get_field_by_where("user_children_max_continuity_record", "end_time", "task_id='{$habit_id}' AND child_id = '{$task_info['child_id']}' ORDER BY id DESC");
        $today_time = strtotime(date("Y-m-d", time()));
        $yesterday_time = $today_time - 3600 * 24;
        if($last_continuity){
            if($last_continuity['end_time'] < $yesterday_time){
                $statistics_info['continuity'] = 0; //已连续0
            }
        }else{
            $statistics_info['continuity'] = 0; //已连续0
        }
       
        if(empty($task_info['head_img_url'])){
            if($task_info['gender'] == 1){
                $task_info['head_img_url'] = get_qiniu_url('FtE8Gn7PqWv0W7aVTuab8RcuGxlm');
            }else{
                $task_info['head_img_url'] = get_qiniu_url('Fsof6z54HyIM401i0amzqo3VzrF3');
            }
        }
        $this->smarty->assign("task_info", $task_info);
        $this->smarty->assign("week_data", $week_data);
        $this->smarty->assign("statistics_info", $statistics_info);
        $this->smarty->assign("habit_id", $habit_id);
		$this->smarty->display("h5/child_habit.html");
	}

    /**
     * 获取所有习惯并获取前3条习惯记录
     * @return [type] [description]
     */
	function get_child_habit_report(){
		$data = file_get_contents("php://input");
    	// echo json_encode(array("code" => 404, "data" => $data));exit;
    	if(!empty($data)){
    		$data = json_decode($data, true);
    		$child_id = $data['childer_id'];

    		//组合data
    		$habit_data = array();

    		$this->load->model('User_child_model');
    		$habit_list = $this->User_child_model->get_habit_by_child($child_id);

    		foreach ($habit_list as $key => $habit) {
    			# code...
    			$habit_record = $this->User_child_model->get_habit_record($habit['id'],3);
    			$records = array();

    			foreach ($habit_record as $key => $record) {
    				# code...
    				$records[] = array(
    					'date' => date("Y-m-d", $record['time']), 
    					'text' => $record['text'],
    					'image' => $record['image'],
    				);
    			}
                $total_stars = 0;
                if(!empty($habit['total_stars'])){
                    $total_stars = $habit['total_stars'];
                }
    			$arrayName = array(
                    'habit_id' => $habit['id'],
    				'habit_name' => $habit['name'],
    				'habit_start_end' => date("Y-m-d", $habit['add_time']) . " - " . date("Y-m-d", time()),
    				'habit_days' => ' 共坚持'.$total_stars.'天',
    				'records' => $records,
    			);

    			$habit_data[] = $arrayName;

    		}
    		$data_obj = array(
                'count' => count($habit_data),
                'list' => $habit_data,
             );
    		
    		echo json_encode(array("code" => 1, "data" => $data_obj));
    	}else{
            echo json_encode(array("code" => 404, "data" => $this->input->post()));
        }
	}



}